home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / f2c_5_92 Folder / f2c_5_92 / libI77 / open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-26  |  3.5 KB  |  193 lines  |  [TEXT/ttxt]

  1. #ifndef THINK_C
  2. #include "sys/types.h"
  3. #ifndef MSDOS
  4. #include "sys/stat.h"
  5. #endif
  6. #endif
  7. #include "f2c.h"
  8. #include "fio.h"
  9. #include "string.h"
  10. #include "fcntl.h"
  11. #ifndef O_WRONLY
  12. #define O_RDONLY 0
  13. #define O_WRONLY 1
  14. #endif
  15.  
  16. extern char *malloc(), *mktemp();
  17. extern FILE *fdopen();
  18. extern integer f_clos();
  19. #ifdef NON_ANSI_RW_MODES
  20. char *r_mode[2] = {"r", "r"};
  21. char *w_mode[2] = {"w", "w"};
  22. #else
  23. char *r_mode[2] = {"rb", "r"};
  24. char *w_mode[2] = {"wb", "w"};
  25. #endif
  26.  
  27. integer f_open(a) olist *a;
  28. {    unit *b;
  29.     int n;
  30.     char buf[256];
  31.     cllist x;
  32. #ifndef MSDOS
  33.     struct stat stb;
  34. #endif
  35.     if(a->ounit>=MXUNIT || a->ounit<0)
  36.         err(a->oerr,101,"open")
  37.     curunit = b = &units[a->ounit];
  38.     if(b->ufd) {
  39.         if(a->ofnm==0)
  40.         {
  41.         same:    if (a->oblnk)
  42.                 b->ublnk = *a->oblnk == 'z' || *a->oblnk == 'Z';
  43.             return(0);
  44.         }
  45. #ifdef MSDOS
  46.         if (b->ufnm
  47.          && strlen(b->ufnm) == a->ofnmlen
  48.          && !strncmp(b->ufnm, b->ufnm, (unsigned)a->ofnmlen))
  49.             goto same;
  50. #else
  51.         g_char(a->ofnm,a->ofnmlen,buf);
  52.         if (inode(buf,&n) == b->uinode && n == b->udev)
  53.             goto same;
  54. #endif
  55.         x.cunit=a->ounit;
  56.         x.csta=0;
  57.         x.cerr=a->oerr;
  58.         if((n=f_clos(&x))!=0) return(n);
  59.         }
  60.     b->url=a->orl;
  61.     b->ublnk = a->oblnk && (*a->oblnk == 'z' || *a->oblnk == 'Z');
  62.     if(a->ofm==0)
  63.     {    if(b->url>0) b->ufmt=0;
  64.         else b->ufmt=1;
  65.     }
  66.     else if(*a->ofm=='f' || *a->ofm == 'F') b->ufmt=1;
  67.     else b->ufmt=0;
  68. #ifdef url_Adjust
  69.     if (b->url && !b->ufmt)
  70.         url_Adjust(b->url);
  71. #endif
  72.     if (a->ofnm) {
  73.         g_char(a->ofnm,a->ofnmlen,buf);
  74.         if (!buf[0])
  75.             err(a->oerr,107,"open")
  76.         }
  77.     else
  78.         sprintf(buf, "fort.%ld", a->ounit);
  79.     b->uscrtch = 0;
  80.     switch(a->osta ? *a->osta : 'u')
  81.     {
  82.     case 'o':
  83.     case 'O':
  84. #ifdef MSDOS
  85.         if(access(buf,0))
  86. #else
  87.         if(stat(buf,&stb))
  88. #endif
  89.             err(a->oerr,errno,"open")
  90.         break;
  91.      case 's':
  92.      case 'S':
  93.         b->uscrtch=1;
  94.         (void) strcpy(buf,"tmp.FXXXXXX");
  95.         (void) mktemp(buf);
  96.         (void) close(creat(buf, 0666));
  97.         break;
  98.     case 'n':
  99.     case 'N':
  100. #ifdef MSDOS
  101.         if(!access(buf,0))
  102. #else
  103.         if(!stat(buf,&stb))
  104. #endif
  105.             err(a->oerr,128,"open")
  106.         /* no break */
  107.     case 'r':    /* Fortran 90 replace option */
  108.     case 'R':
  109.         (void) close(creat(buf, 0666));
  110.         break;
  111.     }
  112.  
  113.     b->ufnm=(char *) malloc((unsigned int)(strlen(buf)+1));
  114.     if(b->ufnm==NULL) err(a->oerr,113,"no space");
  115.     (void) strcpy(b->ufnm,buf);
  116.     b->uend=0;
  117.     b->uwrt = 0;
  118.     if(isdev(buf))
  119.     {    b->ufd = fopen(buf,r_mode[b->ufmt]);
  120.         if(b->ufd==NULL) err(a->oerr,errno,buf)
  121.     }
  122.     else {
  123.         if((b->ufd = fopen(buf, r_mode[b->ufmt])) == NULL) {
  124.             if ((n = open(buf,O_WRONLY)) >= 0) {
  125.                 b->uwrt = 2;
  126.                 }
  127.             else {
  128.                 n = creat(buf, 0666);
  129.                 b->uwrt = 1;
  130.                 }
  131.             if (n < 0
  132.             || (b->ufd = fdopen(n, w_mode[b->ufmt])) == NULL)
  133.                 err(a->oerr, errno, "open");
  134.             }
  135.     }
  136.     b->useek=canseek(b->ufd);
  137. #ifndef MSDOS
  138.     if((b->uinode=inode(buf,&b->udev))==-1)
  139.         err(a->oerr,108,"open")
  140. #endif
  141.     if(a->orl && b->useek) rewind(b->ufd);
  142.     return(0);
  143. }
  144. fk_open(seq,fmt,n) ftnint n;
  145. {    char nbuf[10];
  146.     olist a;
  147.     (void) sprintf(nbuf,"fort.%ld",n);
  148.     a.oerr=1;
  149.     a.ounit=n;
  150.     a.ofnm=nbuf;
  151.     a.ofnmlen=strlen(nbuf);
  152.     a.osta=NULL;
  153.     a.oacc= seq==SEQ?"s":"d";
  154.     a.ofm = fmt==FMT?"f":"u";
  155.     a.orl = seq==DIR?1:0;
  156.     a.oblnk=NULL;
  157.     return(f_open(&a));
  158. }
  159. isdev(s) char *s;
  160. {
  161. #ifdef MSDOS
  162.     int i, j;
  163.  
  164.     i = open(s,O_RDONLY);
  165.     if (i == -1)
  166.         return 0;
  167.     j = isatty(i);
  168.     close(i);
  169.     return j;
  170. #else
  171.     struct stat x;
  172.  
  173.     if(stat(s, &x) == -1) return(0);
  174. #ifdef S_IFMT
  175.     switch(x.st_mode&S_IFMT) {
  176.         case S_IFREG:
  177.         case S_IFDIR:
  178.             return(0);
  179.         }
  180. #else
  181. #ifdef S_ISREG
  182.     /* POSIX version */
  183.     if(S_ISREG(x.st_mode) || S_ISDIR(x.st_mode))
  184.         return(0);
  185.     else
  186. #else
  187.     Help! How does stat work on this system?
  188. #endif
  189. #endif
  190.         return(1);
  191. #endif
  192. }
  193.